package nccloud.web.cmp.informer.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import nc.md.model.MetaDataException;
import nc.md.persist.framework.IMDPersistenceQueryService;
import nc.vo.cmp.informer.InformerAppendVO;
import nc.vo.cmp.informer.InformerReleaseVO;
import nc.vo.cmp.informer.InformerVO;
import nc.vo.ml.NCLangRes4VoTransl;
import nccloud.commons.lang.StringUtils;
import nccloud.dto.cmp.informer.vo.InformerGenerateInfo;
import nccloud.dto.cmp.vo.QueryInfo;
import nccloud.framework.core.exception.ExceptionUtils;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.convert.precision.GridPrecisionOperator;
import nccloud.framework.web.ui.pattern.grid.Grid;
import nccloud.framework.web.ui.pattern.grid.GridOperator;
import nccloud.pubitf.cmp.informer.INCCInformerQueryService;

public class InformerAction extends InformerSuperAction {
	public InformerVO[] getInformerVOsByPKs(String[] pks) throws Exception {
		INCCInformerQueryService service = this.getInformerQueryService();
		InformerVO[] vos = service.queryByPks(pks);
		return vos;
	}

	public InformerReleaseVO[] getOperateReleaseVOs(IRequest request) throws Exception {
		return null;
	}

	public InformerVO[] getOperateInformerVOs(IRequest request) throws Exception {
		QueryInfo info = (QueryInfo) this.getRequestInfo(request, QueryInfo.class);
		if (null == info || null == info.getPks() || info.getPks().length == 0) {
			ExceptionUtils.wrapBusinessException(
					NCLangRes4VoTransl.getNCLangRes().getStrByID("3607nccloud_ncc_0", "03607nccloud_ncc-0241"));
		}

		String[] pks = info.getPks();
		InformerVO[] vos = this.getInformerVOsByPKs(pks);
		if (vos == null || vos.length == 0) {
			ExceptionUtils.wrapBusinessException(
					NCLangRes4VoTransl.getNCLangRes().getStrByID("3607nccloud_ncc_0", "03607nccloud_ncc-0241"));
		}

		return vos;
	}

	public InformerVO[] getRelationVO(InformerVO[] informvos) {
		Map<String, InformerVO> map = new HashMap();

		for (int i = 0; i < informvos.length; ++i) {
			if (informvos[i].getPk_lower() != null) {
				Collection coll = null;

				try {
					coll = ((IMDPersistenceQueryService) ServiceLocator.find(IMDPersistenceQueryService.class))
							.queryBillOfVOByCond(InformerVO.class, "pk_lower='" + informvos[i].getPk_lower() + "'",
									false);
					Iterator var5 = coll.iterator();

					while (var5.hasNext()) {
						InformerVO vo = (InformerVO) var5.next();
						map.put(vo.getPk_informer(), vo);
					}
				} catch (MetaDataException var7) {
					ExceptionUtils.wrapException(var7);
					var7.printStackTrace();
				}
			}
		}

		return (InformerVO[]) map.values().toArray(new InformerVO[0]);
	}

	public String getErrorMessage(InformerAppendVO[] appendvos, InformerVO[] informvos) {
		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < appendvos.length; ++i) {
			if (appendvos[i] != null) {
				String errormessage = appendvos[i].getErrorMessage();
				if (errormessage != null && !"".equals(errormessage)) {
					sb.append(
							NCLangRes4VoTransl.getNCLangRes().getStrByID("3607nccloud_ncc_0", "03607nccloud_ncc-0242"));
					sb.append(informvos[i].getVbillno()).append(" ");
					sb.append(errormessage).append("\n");
				}
			}
		}

		return sb.toString();
	}

	public Grid getResultGrid(String[] status, List<String> pks, IRequest request) {
		Grid result = null;

		try {
			InformerVO[] resultvos = this.getInformerVOsByPKs((String[]) pks.toArray(new String[0]));
			List<InformerVO> list = new ArrayList();
			InformerVO[] var7 = resultvos;
			int var8 = resultvos.length;

			for (int var9 = 0; var9 < var8; ++var9) {
				InformerVO informerVO = var7[var9];
				String[] var11 = status;
				int var12 = status.length;

				for (int var13 = 0; var13 < var12; ++var13) {
					String str = var11[var13];
					if (informerVO.getGenerateflag().equals(str)) {
						list.add(informerVO);
					}
				}
			}

			if (list.size() > 0) {
				QueryInfo info = (QueryInfo) this.getRequestInfo(request, QueryInfo.class);
				GridOperator operator = new GridOperator(info.getPageid());
				result = operator.toGrid(list.toArray(new InformerVO[0]));
				result.getModel().setAreacode("table");
				this.processPrecision(result);
			}

			return result;
		} catch (Exception var15) {
			return null;
		}
	}

	public Grid getResultGrid(IRequest request, String[] status, List<String> pks) {
		Grid result = null;

		try {
			InformerVO[] resultvos = this.getOperateInformerVOs(request);
			List<InformerVO> list = new ArrayList();
			InformerVO[] var7 = resultvos;
			int var8 = resultvos.length;

			for (int var9 = 0; var9 < var8; ++var9) {
				InformerVO informerVO = var7[var9];
				String[] var11 = status;
				int var12 = status.length;

				for (int var13 = 0; var13 < var12; ++var13) {
					String str = var11[var13];
					if (informerVO.getGenerateflag().equals(str) && !pks.contains(informerVO.getPk_informer())) {
						list.add(informerVO);
					}
				}
			}

			if (list.size() > 0) {
				QueryInfo info = (QueryInfo) this.getRequestInfo(request, QueryInfo.class);
				GridOperator operator = new GridOperator(info.getPageid());
				result = operator.toGrid(list.toArray(new InformerVO[0]));
				result.getModel().setAreacode("table");
				this.processPrecision(result);
			}

			return result;
		} catch (Exception var15) {
			return null;
		}
	}

	public Object getResultVO(InformerVO[] informvos, InformerAppendVO[] appendvos, Grid grid) {
		int total = informvos.length;
		int failNum = 0;
		StringBuffer sb = new StringBuffer();
		String errorMessage;
		if (appendvos.length == 1) {
			errorMessage = appendvos[0].getErrorMessage();
			if (StringUtils.isNotEmpty(errorMessage)) {
				String[] str = errorMessage.split("\n");
				failNum = str.length;
				sb.append(errorMessage);
			}
		} else {
			for (int i = 0; i < appendvos.length; ++i) {
				if (appendvos[i] != null) {
					String errormessage = appendvos[i].getErrorMessage();
					if (errormessage != null && !"".equals(errormessage)) {
						sb.append(NCLangRes4VoTransl.getNCLangRes().getStrByID("3607nccloud_ncc_0",
								"03607nccloud_ncc-0242"));
						sb.append(informvos[i].getVbillno()).append(" ");
						sb.append(errormessage).append("\n");
						++failNum;
					}
				}
			}
		}

		errorMessage = sb.toString();
		InformerResult result = new InformerResult();
		result.setTotal(total);
		result.setSuccessNum(total - failNum);
		result.setFailNum(failNum);
		if (errorMessage != null && !errorMessage.equals("")) {
			result.setErrormessage(errorMessage);
		}

		result.setResult(grid);
		return result;
	}

	private void processPrecision(Grid grid) {
		GridPrecisionOperator precision = new GridPrecisionOperator(grid);
		precision.addCurrencyMnyPrecision("moneyy", "pk_currtype");
		precision.addCurrencyMnyPrecision("usemoney", "pk_currtype");
		precision.processPrecision();
	}

	public Grid getGenerateGrid(InformerGenerateInfo geneinfo, String[] status) {
		Grid result = null;

		try {
			InformerVO[] resultvos = this.getInformerVOsByPKs(geneinfo.getPks());
			List<InformerVO> list = new ArrayList();
			InformerVO[] var6 = resultvos;
			int var7 = resultvos.length;

			for (int var8 = 0; var8 < var7; ++var8) {
				InformerVO informerVO = var6[var8];
				String[] var10 = status;
				int var11 = status.length;

				for (int var12 = 0; var12 < var11; ++var12) {
					String str = var10[var12];
					if (informerVO.getGenerateflag().equals(str)) {
						list.add(informerVO);
					}
				}
			}

			if (list.size() > 0) {
				GridOperator operator = new GridOperator(geneinfo.getPageid());
				result = operator.toGrid(list.toArray(new InformerVO[0]));
				result.getModel().setAreacode("table");
				this.processPrecision(result);
			}

			return result;
		} catch (Exception var14) {
			return null;
		}
	}
}